package com.egg.ylt.Utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.egg.ylt.R;
import com.egg.ylt.activity.ACT_ConfirmPayment;
import com.egg.ylt.activity.ACT_MealCardDetails;
import com.egg.ylt.activity.ACT_ServiceItem;
import com.egg.ylt.pojo.NatatoriumByidEntity;

/**
 * Created by lijiateng on 2019/2/25.
 */

public class ShopDetailUtil {

    public static final int TYPE_SERVICE = 0;
    public static final int TYPE_CARD = 1;
    public static final int TYPE_PROJECT = 2;

    public static View inflateProjectItem(Context context, NatatoriumByidEntity.ProjectListBean projectListBean) {
        LinearLayout serviceLayout = (LinearLayout) LayoutInflater.from(context)
                .inflate(R.layout.item_shop_detail_project, null);

        // 项目名称
        TextView tvCardName = serviceLayout.findViewById(R.id.tv_project_name);
        StringBuffer sb = new StringBuffer();
        sb.append("【").append(projectListBean.getImageName()).append("】").append(projectListBean.getRemark());
        tvCardName.setText(sb.toString());

        // 项目图片
        ImageView ivLogo = serviceLayout.findViewById(R.id.iv_project_logo);
        Glide.with(context)
                .load(projectListBean.getImageUrl())
                .placeholder(R.mipmap.ic_serve_default)
                .error(R.mipmap.ic_serve_default)
                .into(ivLogo);

        return serviceLayout;
    }

    public static View inflateTimesCardItem(final Context context,
                                            final NatatoriumByidEntity.CardListBean cardListBean,
                                            final String openTime) {
        final LinearLayout timesCardLayout = (LinearLayout) LayoutInflater.from(context)
                .inflate(R.layout.item_shop_detail_times_card, null);

        // 图标
        ImageView ivLogo = timesCardLayout.findViewById(R.id.iv_logo);
        Glide.with(context)
                .load(cardListBean.getImageUrl())
                .placeholder(R.mipmap.ic_serve_default)
                .error(R.mipmap.ic_serve_default)
                .into(ivLogo);

        // 名称
        TextView tvCardName = timesCardLayout.findViewById(R.id.tv_card_name);
        tvCardName.setText(cardListBean.getName());

        // 价格
        TextView tvCardPrice = timesCardLayout.findViewById(R.id.tv_card_price);
        tvCardPrice.setText(String.format("%s", cardListBean.getPrice()));

        // 原价
        TextView tvCardOriginPrice = timesCardLayout.findViewById(R.id.tv_card_origin_price);
        tvCardOriginPrice.setText(String.format("%s", cardListBean.getTotalPrice()));
        tvCardOriginPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

        // 已售
        TextView tvCardSold = timesCardLayout.findViewById(R.id.tv_already_sold);
        tvCardSold.setText("已售" + cardListBean.getSellNum());


        TextView tvBuyCard = timesCardLayout.findViewById(R.id.tv_buy_card);
        tvBuyCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ACT_ConfirmPayment.class);
                Bundle bundle = new Bundle();
                bundle.putString("cardId", cardListBean.getId());
                bundle.putString("cardName", cardListBean.getName());
                bundle.putString("price", cardListBean.getPrice());
                bundle.putString("endDate", cardListBean.getEndDate());
                bundle.putString("shopName", cardListBean.getShopName());
                bundle.putString("shopId", cardListBean.getShopId());
                bundle.putString("applyType", "2");
                bundle.putString("cardImageUrl", cardListBean.getImageUrl());
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });

        // 点击跳转次卡详情
        timesCardLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ACT_MealCardDetails.class);
                intent.putExtra("cardId", cardListBean.getId());
                intent.putExtra("shopId", cardListBean.getShopId());
                intent.putExtra("shopName", cardListBean.getShopName());
                intent.putExtra("cardImageUrl", cardListBean.getImageUrl());
                intent.putExtra("openTime", openTime);
                context.startActivity(intent);
            }
        });

        return timesCardLayout;
    }

    public static View inflateServiceItem(final Context context,
                                          final NatatoriumByidEntity.ServerListBean serverListBean,
                                          final String shopId,
                                          final String storePhone) {
        LinearLayout serviceLayout = (LinearLayout) LayoutInflater.from(context)
                .inflate(R.layout.item_shop_detail_service, null);

        // 图标
        ImageView ivLogo = serviceLayout.findViewById(R.id.iv_service_logo);
        Glide.with(context)
                .load(serverListBean.getImageUrl())
                .placeholder(R.mipmap.ic_serve_default)
                .error(R.mipmap.ic_serve_default)
                .into(ivLogo);

        // 名称
        TextView tvCardName = serviceLayout.findViewById(R.id.tv_service_name);
        tvCardName.setText(serverListBean.getName());

        // 服务项
        TextView serviceFeature = serviceLayout.findViewById(R.id.tv_service_feature);
        serviceFeature.setText(serverListBean.getInfo());

        // 预约数
        TextView tvCardPrice = serviceLayout.findViewById(R.id.tv_service_appoint);
        tvCardPrice.setText("已预约 " + serverListBean.getReserveNum());


        serviceLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ACT_ServiceItem.class);
                intent.putExtra("id", serverListBean.getId());
                intent.putExtra("shopId", shopId);
                intent.putExtra("storePhone", storePhone);
                intent.putExtra("serviceImageUrl", serverListBean.getImageUrl());
                context.startActivity(intent);
            }
        });

        return serviceLayout;
    }

    public static View inflateEmptyLayout(Context context, int type) {

        LinearLayout llEmpty = (LinearLayout) LayoutInflater.from(context)
                .inflate(R.layout.layout_shop_detail_empty, null);
        ImageView ivLogo = llEmpty.findViewById(R.id.iv_empty_logo);
        TextView tvEmpty = llEmpty.findViewById(R.id.tv_empty_text);

        switch (type) {
            case TYPE_SERVICE:
                ivLogo.setImageResource(R.mipmap.ic_shop_detail_service_empty);
                tvEmpty.setText("暂无服务数据");
                break;
            case TYPE_CARD:
                ivLogo.setImageResource(R.mipmap.ic_shop_detail_card_empty);
                tvEmpty.setText("暂无次卡数据");
                break;
            case TYPE_PROJECT:
                tvEmpty.setText("暂无项目数据");
                break;
        }

        return llEmpty;
    }

}
